//
//  APPRefresh.swift
//  APP
//
//  Created by Rdxer on 2017/12/22.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import MJRefresh

extension UIScrollView{

    /// 头部 下拉刷新
    @discardableResult
    func prepareRefreshHeader(refreshingTarget: Any, refreshingAction: Selector)->APPRefreshHeader{
        let res = APPRefreshHeader.init(refreshingTarget: refreshingTarget, refreshingAction: refreshingAction)
        self.mj_header = res
        return res
    }


    /// 底部 上拉加载更多
    @discardableResult
    func prepareRefreshFooter(refreshingTarget: Any, refreshingAction: Selector)->APPRefreshAutoFooter{
        let res = APPRefreshAutoFooter.init(refreshingTarget: refreshingTarget, refreshingAction: refreshingAction)
        self.mj_footer = res
        return res
    }
}

@objc
protocol XXPrepareRefreshHeaderProtocol :NSObjectProtocol{
    @objc optional func loadData()
    @objc optional func didPrepareHeader()
    
}

@objc
protocol XXPrepareRefreshFooterProtocol :NSObjectProtocol{
    @objc optional func loadMoreData()
    @objc optional func didPrepareFooter()
    
}

extension XXPrepareRefreshHeaderProtocol {
    /// 头部 下拉刷新
    @discardableResult
    public func prepareRefreshHeader(_ scrollView:UIScrollView)->APPRefreshHeader?{
        if self.responds(to: #selector(loadData)) {
            let v = scrollView.prepareRefreshHeader(refreshingTarget: self, refreshingAction: #selector(loadData))
            self.didPrepareHeader?()
            return v
        }
        return nil
    }
    @discardableResult
    public func prepareRefreshHeaderIfNeed(_ scrollView:UIScrollView)->APPRefreshHeader?{
        if scrollView.header_safe != nil{
            return scrollView.header_safe as? APPRefreshHeader
        }
        return prepareRefreshHeader(scrollView)
    }
}

extension XXPrepareRefreshFooterProtocol {
    /// 底部 上拉加载更多
    @discardableResult
    public func prepareRefreshFooter(_ scrollView:UIScrollView)->APPRefreshAutoFooter?{
        if self.responds(to: #selector(loadMoreData)) {
            let v = scrollView.prepareRefreshFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
            self.didPrepareFooter?()
            return v
        }
        return nil
    }
    
    /// 底部 上拉加载更多
    @discardableResult
    public func prepareRefreshFooterIfNeed(_ scrollView:UIScrollView)->APPRefreshAutoFooter?{
        if scrollView.footer_safe != nil{
            return scrollView.footer_safe as? APPRefreshAutoFooter
        }
        return prepareRefreshFooter(scrollView)
    }
}

@objc
protocol XXPrepareRefreshHeaderAndFooterProtocol:XXPrepareRefreshHeaderProtocol , XXPrepareRefreshFooterProtocol {

}

extension XXPrepareRefreshHeaderAndFooterProtocol{
    public func prepareRefreshHeaderFooterIfNeed(_ scrollView:UIScrollView)->(
        APPRefreshHeader?,
        APPRefreshAutoFooter?
        ){
        return (
            prepareRefreshHeader(scrollView),
            prepareRefreshFooter(scrollView)
        )
    }
}

extension UIViewController:XXPrepareRefreshHeaderAndFooterProtocol{

}




